ALTER TABLE APIAccessKey
ADD AllowImpersonation BIT NOT NULL DEFAULT 0
GO

ALTER TABLE LineSegmentAttributes
ADD FromBus VARCHAR(150) NULL DEFAULT(NULL)
GO

ALTER TABLE LineSegmentAttributes
ADD ToBus VARCHAR(150) NULL DEFAULT(NULL)
GO

ALTER VIEW LineSegment AS
	SELECT 
		AssetID AS ID,
		AssetKey,
		Length,
		R0,
		X0,
		R1,
		X1,
		ThermalRating,
		Description,
		AssetName,
		VoltageKV,
		AssetTypeID,
		Spare,
        IsEnd,
        FromBus,
        ToBus
	FROM Asset JOIN LineSegmentAttributes ON Asset.ID = LineSegmentAttributes.AssetID
GO

DROP TRIGGER TR_INSERT_LineSegment
GO

CREATE TRIGGER TR_INSERT_LineSegment ON LineSegment
INSTEAD OF INSERT AS 
BEGIN
	INSERT INTO Asset (AssetKey, AssetTypeID, Description, AssetName, VoltageKV, Spare)
		SELECT 
			AssetKey AS AssetKey,
			(SELECT ID FROM AssetType WHERE Name = 'LineSegment') AS AssetTypeID,
			Description AS Description,
			AssetName AS AssetName,
			VoltageKV AS VoltageKV,
			Spare AS Spare
	FROM INSERTED

	INSERT INTO LineSegmentAttributes (AssetID, Length, R0, X0, R1, X1, ThermalRating, IsEnd, FromBus, ToBus)
		SELECT 
			(SELECT ID FROM Asset WHERE AssetKey = INSERTED.AssetKey) AS AssetID,
			Length AS Length,
			R0 AS R0,
			X0 AS X0,
			R1 AS R1,
			X1 AS X1,
			ThermalRating AS ThermalRating,
            IsEnd AS IsEnd,
            FromBus AS FromBus,
            ToBus AS ToBus
	FROM INSERTED

END
GO

DROP TRIGGER TR_UPDATE_LineSegment
GO

CREATE TRIGGER TR_UPDATE_LineSegment ON LineSegment
INSTEAD OF UPDATE AS
BEGIN
IF (UPDATE(AssetKey) OR UPDATE(Description) OR UPDATE (AssetName) OR UPDATE(VoltageKV) OR Update(Spare))
	BEGIN
		UPDATE Asset
		SET
			Asset.AssetKey = INSERTED.AssetKey,
			Asset.Description = INSERTED.Description,
			Asset.AssetName = INSERTED.AssetName,
			Asset.VoltageKV  = INSERTED.VoltageKV,
			Asset.Spare = INSERTED.Spare
		FROM
			ASSET 
		INNER JOIN
			INSERTED
		ON 
			INSERTED.ID = ASSET.ID;
	END
	UPDATE LineSegmentAttributes
		SET
			LineSegmentAttributes.R0 = INSERTED.R0,
			LineSegmentAttributes.X0 = INSERTED.X0,
			LineSegmentAttributes.R1 = INSERTED.R1,
			LineSegmentAttributes.X1 = INSERTED.X1,
            LineSegmentAttributes.Length = INSERTED.Length,
			LineSegmentAttributes.ThermalRating = INSERTED.ThermalRating,
            LineSegmentAttributes.IsEnd = INSERTED.IsEnd,
            LineSegmentAttributes.FromBus = INSERTED.FromBus,
            LineSegmentAttributes.ToBus = INSERTED.ToBus
		FROM
			LineSegmentAttributes 
	INNER JOIN
		INSERTED
	ON 
		INSERTED.ID = LineSegmentAttributes.AssetID;
END
GO

ALTER TABLE AssetLocation
ADD CONSTRAINT UC_AssetLocation UNIQUE(AssetID, LocationID)
GO

ALTER TABLE Channel
ADD Trend BIT NOT NULL DEFAULT 0
GO

UPDATE ValueList
SET AltValue = Value
WHERE GroupID = (SELECT ID FROM ValueListGroup WHERE Name = 'Unit') AND AltValue IS NULL
GO

ALTER TABLE ChannelGroupType
ADD Unit VARCHAR(200) NOT NULL DEFAULT ('')
GO

UPDATE ChannelGroupType
SET Unit = (SELECT AltValue FROM ValueList WHERE ID = UnitID)
GO

ALTER TABLE ChannelGroupType
DROP COLUMN UnitID
GO

ALTER TABLE UserAccountScheduledEmailType
ADD Approved BIT NOT NULL DEFAULT 0
GO

CREATE VIEW SubscribeScheduledEmails
AS
SELECT
    UserAccountScheduledEmailType.ID,
    UserAccountScheduledEmailType.Approved,
    ScheduledEmailType.ID as ScheduledEmailID,
    UserAccount.FirstName as FirstName,
    UserAccount.LastName as LastName,
    UserAccount.Email as Email,
    AssetGroup.Name as AssetGroup
FROM
    UserAccountScheduledEmailType JOIN
    ScheduledEmailType ON ScheduledEmailType.ID = UserAccountScheduledEmailType.ScheduledEmailTypeID JOIN
    UserAccount ON UserAccount.ID = UserAccountScheduledEmailType.UserAccountID JOIN
    AssetGroup ON AssetGroup.ID = UserAccountScheduledEmailType.AssetGroupID
GO

CREATE TABLE ChannelTemplateFile
(
    ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    Name VARCHAR(200) NOT NULL,
    FileBlob VARBINARY(MAX) NOT NULL,
    FileName VARCHAR(MAX) NOT NULL
)
GO

ALTER TABLE EventType
ADD ShowInFilter BIT NOT NULL Default(1)
GO

ALTER TABLE EventType
ADD Category VARCHAR(200) NULL
GO

CREATE TABLE EventTypeAssetType
(
    ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    EventTypeID INT NOT NULL REFERENCES EventType(ID),
    AssetTypeID INT NOT NULL REFERENCES AssetType(ID),
    CONSTRAINT UC_EventTypeAssetType UNIQUE(EventTypeID, AssetTypeID)
)
GO

CREATE TABLE MATLABAnalytic
(
    ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    AssemblyName VARCHAR(MAX) NOT NULL,
    MethodName VARCHAR(MAX) NOT NULL,
    SettingSQL VARCHAR(MAX) NOT NULL DEFAULT '',
    LoadOrder INT NOT NULL DEFAULT 0
)
GO

CREATE TABLE MATLABAnalyticAssetType
(
    ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    MATLABAnalyticID INT NOT NULL REFERENCES MATLABAnalytic(ID),
    AssetTypeID INT NOT NULL REFERENCES AssetType(ID)
)
GO

CREATE TABLE MATLABAnalyticEventType
(
    ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    MATLABAnalyticID INT NOT NULL REFERENCES MATLABAnalytic(ID),
    EventTypeID INT NOT NULL REFERENCES EventType(ID)
)
GO

CREATE TABLE EventTag
(
    ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    Name VARCHAR(200) NOT NULL UNIQUE,
    Description VARCHAR(MAX) NULL,
    ShowInFilter BIT NOT NULL Default(1),
)
GO

CREATE TABLE EventEventTag
(
    ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    EventID INT NOT NULL REFERENCES Event(ID),
    EventTagID INT NOT NULL REFERENCES EventTag(ID),
    TagData VARCHAR(MAX) NULL
)
GO

ALTER TABLE FaultCurve
ADD PathNumber INT NOT NULL CONSTRAINT DF_FaultCurve_PathNumber DEFAULT(1)
GO
ALTER TABLE FaultCurve
DROP CONSTRAINT DF_FaultCurve_PathNumber
GO

ALTER TABLE FaultSummary
ADD PathNumber INT NOT NULL CONSTRAINT DF_FaultSummary_PathNumber DEFAULT(1)
GO
ALTER TABLE FaultSummary
DROP CONSTRAINT DF_FaultSummary_PathNumber
GO

ALTER TABLE FaultSummary
ADD LineSegmentID INT NOT NULL CONSTRAINT DF_FaultSummary_LineSegmentID DEFAULT(0)
GO
ALTER TABLE FaultSummary
DROP CONSTRAINT DF_FaultSummary_LineSegmentID
GO
UPDATE FaultSummary
SET LineSegmentID = LineSegment.ID
FROM
    FaultSummary JOIN
    Event ON FaultSummary.EventID = Event.ID JOIN
    Asset Line ON Event.AssetID = Line.ID JOIN
    AssetRelationship ON Line.ID IN (AssetRelationship.ParentID, AssetRelationship.ChildID) JOIN
    AssetRelationshipType ON
        AssetRelationship.AssetRelationshipTypeID = AssetRelationshipType.ID AND
        AssetRelationshipType.Name = 'Line-LineSegment' JOIN
    Asset LineSegment ON LineSegment.ID IN (AssetRelationship.ParentID, AssetRelationship.ChildID) JOIN
    AssetType ON
        LineSegment.AssetTypeID = AssetType.ID AND
        AssetType.Name = 'LineSegment'
GO
ALTER TABLE FaultSummary
ADD FOREIGN KEY(LineSegmentID) REFERENCES Asset(ID)
GO

ALTER TABLE FaultSummary
ADD LineSegmentDistance FLOAT NOT NULL CONSTRAINT DF_FaultSummary_LineSegmentDistance DEFAULT(0)
GO
ALTER TABLE FaultSummary
DROP CONSTRAINT DF_FaultSummary_LineSegmentDistance
GO
UPDATE FaultSummary
SET LineSegmentDistance = Distance
GO

ALTER TABLE NoteType
ADD Label VARCHAR(200)
GO
UPDATE NoteType
SET Label = Name
GO
UPDATE NoteType
SET Label = 'Substation'
WHERE Name = 'Location'
GO

CREATE VIEW ActiveScheduledSubscription AS
    SELECT 
        UserAccountScheduledEmailType.ID AS UserAccountScheduledEmailID,
        UserAccountScheduledEmailType.UserAccountID AS UserAccountID,
        AssetGroup.Name AS AssetGroup,
        ScheduledEmailType.Name AS EmailName,
        EmailCategory.Name AS Category,
        ScheduledEmailType.ID AS ScheduledEmailTypeID,
        UserAccount.Name AS UserName,
        UserAccount.Email AS Email
    FROM UserAccountScheduledEmailType LEFT JOIN
        AssetGroup ON AssetGroup.ID = UserAccountScheduledEmailType.AssetGroupID LEFT JOIN
        ScheduledEmailType ON UserAccountScheduledEmailType.ScheduledEmailTypeID = ScheduledEmailType.ID LEFT JOIN
        EmailCategory ON EmailCategory.ID = ScheduledEmailType.EmailCategoryID LEFT JOIN
        UserAccount ON UserAccount.ID = UserAccountScheduledEmailType.UserAccountID
GO

ALTER VIEW MeterAssetGroupView
AS
SELECT
    MeterAssetGroup.ID,
    Meter.Name AS MeterName,
    Meter.ID AS MeterID,
    AssetGroupID,
    Location.Name AS Location,
    AssetGroup.Name,
    AssetGroup.DisplayDashboard
FROM
    MeterAssetGroup JOIN
    Meter ON MeterAssetGroup.MeterID = Meter.ID JOIN
    AssetGroup ON MeterAssetGroup.AssetGroupID = AssetGroup.ID JOIN
    Location ON Meter.LocationID = Location.ID
GO

 ALTER VIEW AssetAssetGroupView
 AS
 SELECT
    AssetAssetGroup.ID,
    Asset.AssetKey AS Assetname,
    Asset.AssetName AS LongAssetName,
    Asset.ID AS AssetID,
	AssetType.Name AS AssetType,
	(SELECT Top 1 LocationKey FROM Location WHERE Location.ID IN (SELECT LocationID FROM AssetLocation WHERE AssetLocation.AssetID = Asset.ID)) AS AssetLocation,
    AssetGroupID,
    AssetGroup.Name,
    AssetGroup.DisplayDashboard
 FROM
    AssetAssetGroup JOIN
    Asset ON AssetAssetGroup.AssetID = Asset.ID LEFT JOIN
    AssetGroup ON AssetAssetGroup.AssetGroupID = AssetGroup.ID JOIN
    AssetType ON Asset.AssetTypeID = AssetType.ID
GO

CREATE VIEW ScheduledEmailDataSourceEmailTypeView 
AS
SELECT 
    ScheduledEmailDataSourceEmailType.*,
    ScheduledEmailDatasource.Name AS ScheduledEmailDataSourceName
FROM
    ScheduledEmailDataSourceEmailType LEFT JOIN 
    ScheduledEmailDatasource ON ScheduledEmailDataSourceEmailType.ScheduledEmailDataSourceID = ScheduledEmailDatasource.ID
GO

INSERT INTO ScheduledEmailDatasource (Name, AssemblyName, TypeName,ConfigUI) VALUES
('AZURE','openXDA.NotificationDataSources.dll','openXDA.NotificationDataSources.AzureDataSource','azure')
GO

/* Other and Test event types are always allowed since those apply to all asset types in general */
INSERT INTO EventTypeAssetType (EventTypeID,AssetTypeID) VALUES
	((SELECT ID FROM EventType WHERE Name = 'Fault'),(SELECT ID FROM AssetType WHERE Name = 'Transformer')),
	((SELECT ID FROM EventType WHERE Name = 'Fault'),(SELECT ID FROM AssetType WHERE Name = 'Line')),
	((SELECT ID FROM EventType WHERE Name = 'RecloseIntoFault'),(SELECT ID FROM AssetType WHERE Name = 'Breaker')),
	((SELECT ID FROM EventType WHERE Name = 'BreakerOpen'),(SELECT ID FROM AssetType WHERE Name = 'Breaker')),
	((SELECT ID FROM EventType WHERE Name = 'Interruption'),(SELECT ID FROM AssetType WHERE Name = 'Bus')),
	((SELECT ID FROM EventType WHERE Name = 'Sag'),(SELECT ID FROM AssetType WHERE Name = 'Bus')),
	((SELECT ID FROM EventType WHERE Name = 'Swell'),(SELECT ID FROM AssetType WHERE Name = 'Bus')),
	((SELECT ID FROM EventType WHERE Name = 'Transient'),(SELECT ID FROM AssetType WHERE Name = 'Bus')),
	((SELECT ID FROM EventType WHERE Name = 'Interruption'),(SELECT ID FROM AssetType WHERE Name = 'CapacitorBank')),
	((SELECT ID FROM EventType WHERE Name = 'Sag'),(SELECT ID FROM AssetType WHERE Name = 'CapacitorBank')),
	((SELECT ID FROM EventType WHERE Name = 'Swell'),(SELECT ID FROM AssetType WHERE Name = 'CapacitorBank')),
	((SELECT ID FROM EventType WHERE Name = 'Transient'),(SELECT ID FROM AssetType WHERE Name = 'CapacitorBank')),
	((SELECT ID FROM EventType WHERE Name = 'Snapshot'),(SELECT ID FROM AssetType WHERE Name = 'Bus')),
	((SELECT ID FROM EventType WHERE Name = 'Snapshot'),(SELECT ID FROM AssetType WHERE Name = 'Breaker'))
GO

ALTER TABLE LSCVSAccount
ADD ChannelID INT NOT NULL DEFAULT(1)
GO

CREATE TABLE [SEBrowser.WidgetCategory] (
    ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    Name varchar(50) NOT NULL,
    OrderBy INT NOT NULL
)
GO

INSERT [SEBrowser.WidgetCategory] (Name, OrderBy) VALUES ('Waveform Analysis', 1)
GO
INSERT [SEBrowser.WidgetCategory] (Name, OrderBy) VALUES ('Fault', 2)
GO
INSERT [SEBrowser.WidgetCategory] (Name, OrderBy) VALUES ('Correlating Events', 3)
GO
INSERT [SEBrowser.WidgetCategory] (Name, OrderBy) VALUES ('Configuration', 4)
GO
INSERT [SEBrowser.WidgetCategory] (Name, OrderBy) VALUES ('All', 5)
GO

CREATE TABLE [SEBrowser.Widget] (
    ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    Name varchar(50) NOT NULL DEFAULT(1),
    Setting varchar(max) NOT NULL Default(''),
    Enabled BIT NOT NULL
)
GO

INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('EventSearchOpenSEE', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('EventSearchFaultSegments', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('EventSearchAssetVoltageDisturbances', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('TVAESRIMap', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('TVAFaultInfo', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('LineParameters', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('TVALightning', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('TVAStructureInfo', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('AssetHistoryTable', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('AssetHistoryStats', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('EventSearchCorrelatedSags', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('TVASIDA', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('TVASOE', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('TVASLC', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('TVAPQWeb', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('HECCOIR', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('pqi', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('EventSearchFileInfo', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('EventSearchNoteWindow', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('EventSearchRelayPerformance', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('EventSearchBreakerPerformance', 1)
GO
INSERT [SEBrowser.Widget] (Name, Enabled) VALUES ('EventSearchCapBankAnalyticOverview', 1)
GO


CREATE TABLE [SEBrowser.WidgetWidgetCategory] (
    ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    WidgetID INT NOT NULL REFERENCES [SEBrowser.Widget](ID),
    CategoryID INT NOT NULL REFERENCES [SEBrowser.WidgetCategory](ID),
    CONSTRAINT UC_WidgetWidgetCategory UNIQUE(WidgetID, CategoryID)
)
GO

INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (1, 1)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (2, 1)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (3, 1)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (4, 2)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (5, 2)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (6, 2)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (7, 2)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (8, 2)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (9, 2)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (10, 2)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (11, 3)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (12, 3)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (13, 3)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (14, 3)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (15, 3)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (16, 3)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (17, 4)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (18, 4)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (19, 4)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (1, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (2, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (3, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (4, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (5, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (6, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (7, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (8, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (9, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (10, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (11, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (12, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (13, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (14, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (15, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (16, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (17, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (18, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (19, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (20, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (21, 5)
GO
INSERT [SEBrowser.WidgetWidgetCategory] (WidgetID, CategoryID) VALUES (22, 5)
GO

CREATE VIEW [SEBrowser.WidgetView] AS
	SELECT 
	Widget.*,
    Widgetcategory.ID AS CategoryID
FROM
	[SEBrowser.WidgetWidgetCategory] LEFT JOIN 
    [SEBrowser.Widget] Widget ON Widget.ID = [SEBrowser.WidgetWidgetCategory].WidgetID LEFT JOIN 
    [SEBrowser.WidgetCategory] Widgetcategory ON Widgetcategory.ID = [SEBrowser.WidgetWidgetCategory].CategoryID
GO